package cn.cqnu.controller;

import cn.cqnu.domin.dto.LoginDto;
import cn.cqnu.domin.vo.ResponseResult;
import cn.cqnu.global.SystemReturnEnum;
import cn.cqnu.service.ManagerService;
import cn.cqnu.service.StudentService;
import cn.hutool.core.util.StrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @作者：张世豪
 * @version：1.0
 */
@RestController
public class LoginController {


    @Autowired
    private StudentService studentService;

    @Autowired
    private ManagerService managerService;

    @PostMapping("/login")
    public ResponseResult login(@RequestBody LoginDto dto){

        // 请求数据完整性判断
        if(dto.getId() == null || StrUtil.isBlank(dto.getPassword()) || dto.getType()==null)
            return ResponseResult.error(SystemReturnEnum.REQUEST_PARAMS_INCOMPLETE);


        // 判断是尝试登录学生还是管理员
        if(dto.getType() == 0)
            return studentService.login(dto.getId(), dto.getPassword());

        return managerService.login(dto.getId(), dto.getPassword());
    }


}
